/*

  Director:Menus.Internal.c.Info

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include "oslib/os.h"
#include "oslib/wimp.h"
#include "oslib/osfile.h"
#include "oslib/osfscontrol.h"
#include "oslib/uri.h"
#include "oslib/messagetrans.h"


#define E(a) if ((a)!=NULL) {wimp_error(a,1);exit(EXIT_FAILURE);}


typedef struct filer_opendir filer_opendir;
typedef struct filer_run filer_run;


struct filer_opendir
{
  char  filer [14];
  char  dir [256-14];
};

struct filer_run
{
  char  filer [10];
  char  file [256-10];
};


extern char _version [10];
extern char _date [11];
extern char _year [4];
wimp_t      task_handle;
char        *task_name;
osbool      task_quit;
wimp_block  *block;

messagetrans_control_block msgtrans_block;
char                       * msgtrans_memory;


void wimp_error
(
  os_error * err,
  int     flags
)
{
  if (xwimp_report_error (err, flags, task_name, NULL) !=0)
  {
    printf ("%s\n\nwimp_error failed", task_name);
    exit (EXIT_FAILURE);
  }
}


void task_init
(
  void
)
{
  os_error * err;

  err = xwimp_initialise (wimp_VERSION_RO3, task_name, NULL, NULL,
    &task_handle);
  E (err);
}


void task_close
(
  void
)
{
  os_error     * err;

  err = xwimp_close_down (NULL);
  E (err);
}


void init
(
  void
)
{
  os_error        * err;
  char            varname [] ="Director$Window\0";
  int             proginfo_whandle;
  wimp_icon_state icon_block;
  char            input [32];
  char            output [32] = "\0";

  task_init ();

    //  Get the window handle of the window
  err = xos_read_var_val ( (char *) varname, (char *) &proginfo_whandle, 4,
    NULL, os_VARTYPE_NUMBER, NULL, NULL, NULL);
  E (err);

    //  Get icon information
  icon_block.w = (wimp_w) proginfo_whandle;
  icon_block.i = (wimp_i) 2;

  err = xwimp_get_icon_state (&icon_block);
  E (err);

    //  Write text string
  err = xmessagetrans_lookup (&msgtrans_block, "Info_Author", input,
    32, NULL, NULL, NULL, NULL, NULL, NULL);
  E (err);

  snprintf( output, 32, input, _year);

  strcpy (icon_block.icon.data.indirected_text.text, output);

    //  Get icon information
  icon_block.i = (wimp_i) 4;

  err = xwimp_get_icon_state (&icon_block);
  E (err);

    //  Write text string
  err = xmessagetrans_lookup (&msgtrans_block, "Info_Version", input,
    32, _version, NULL, NULL, NULL, NULL, NULL);
  E (err);

  snprintf( output, 32, input, _version, _date);

  strcpy (icon_block.icon.data.indirected_text.text, output);

  task_close ();
}


void email
(
  void
)
{
  os_error  * err;
  char      command [] =
    "DirectorURL mailto:director-users@lists.sourceforge.net";

  task_init ();

  err = xwimp_start_task (command, NULL);
  E (err);

  task_close ();
}


void help
(
  void
)
{
  os_error          *err;
  filer_opendir     filer_opendir_block;
  filer_run         filer_run_block;
  fileswitch_object_type obj_type;
  bool              status = FALSE;
  os_error          myerr = {0, "Unable to find the help files for Director\n"};

  task_init ();

  strcpy (filer_opendir_block.filer, "Filer_OpenDir ");
  strcpy (filer_run_block.filer, "Filer_Run ");

  err = xosfscontrol_canonicalise_path ("<Director$Dir>.^.Docs",
    filer_opendir_block.dir, NULL, NULL, 256-14-1, NULL);

  if (err == NULL)
  {
    err = xosfile_read_stamped_no_path (filer_opendir_block.dir,
      &obj_type, NULL, NULL, NULL, NULL, NULL);
    if ((err == NULL) && (obj_type != osfile_NOT_FOUND))
    {
      err = xwimp_start_task ( (char *) &filer_opendir_block, NULL);
      E (err);
      status = TRUE;
    }
  }

  err = xosfscontrol_canonicalise_path ("<Director$Dir>.Docs",
    filer_opendir_block.dir, NULL, NULL, 256-14-1, NULL);

  if (err == NULL)
  {
    err = xosfile_read_stamped_no_path (filer_opendir_block.dir,
      &obj_type, NULL, NULL, NULL, NULL, NULL);
    if ((err == NULL) && (obj_type != osfile_NOT_FOUND))
    {
      err = xwimp_start_task ( (char *) &filer_opendir_block, NULL);
      E (err);
      status = TRUE;
    }
  }

  err = xosfscontrol_canonicalise_path ("<Director$Dir>.!Help",
    filer_run_block.file, NULL, NULL, 256-10-1, NULL);

  if (err == NULL)
  {
    err = xosfile_read_stamped_no_path (filer_run_block.file,
      &obj_type, NULL, NULL, NULL, NULL, NULL);
    if ((err == NULL) && (obj_type != osfile_NOT_FOUND))
    {
      err = xwimp_start_task ( (char *) &filer_run_block, NULL);
      E (err);
      status = TRUE;
    }
  }


  if (status != TRUE )
  {
      // We were unable to launch the help, so tell the user
    E (&myerr);
  }

  task_close ();
}


void help_msg
(
  void
)
{
  os_error  * err;
  byte      command [] = "This shows information about Director\0";

  err = xos_set_var_val ("Director$Window$Help", command, sizeof (command),
    NULL, os_VARTYPE_STRING, NULL, NULL);
  E (err);
}


void web
(
  void
)
{
  os_error  * err;
  char      command [] = "DirectorURL http://director.sourceforge.net";

  task_init ();

  err = xwimp_start_task (command, NULL);
  E (err);

  task_close ();
}


int main
(
  void
)
{
  os_error                * err;
  int                     buttons;
  int                     ihandle;
  messagetrans_file_flags flags;
  int                     size;

  task_name = malloc (32);
  strcpy (task_name, "Director:Info");

  err = xmessagetrans_file_info ("<Director$Resources>Messages", &flags, &size);
  E (err);

  if ( flags == messagetrans_DIRECT_ACCESS)
  {
    msgtrans_memory = NULL;
  }
  else
  {
    msgtrans_memory = malloc (size);
    if (msgtrans_memory == NULL)
    {
      printf ("%s\n\nI was unable to allocate enough memory for the Messages file.\n", task_name);
      exit (EXIT_FAILURE);
    }
  }

  err = xmessagetrans_open_file (&msgtrans_block,
    "<Director$Resources>Messages", msgtrans_memory);
  E (err);

  err = xmessagetrans_lookup (&msgtrans_block, "Info_TaskName", task_name,
    32, NULL, NULL, NULL, NULL, NULL, NULL);
  E (err);


    //  Are we being initialised?
  err = xos_read_var_val ("Director$Buttons", (char *) &buttons, 4, NULL,
    os_VARTYPE_NUMBER, NULL, NULL, NULL);
  E (err);

  if (buttons == 0xFFFFFF06)
  {
    init ();
  }
  else if (buttons == 0xFFFFFF09)
  {
    help_msg ();
  }
  {
      //  Get the icon handle
    err = xos_read_var_val ("Director$Icon", (char *) &ihandle, 4, NULL,
      os_VARTYPE_NUMBER, NULL, NULL, NULL);
    E (err);

    switch (ihandle)
    {
      case 11:
        //  Help
        help ();
        break;
      case 12:
        //  Email
        email ();
        break;
      case 13:
        //  Web
        web ();
        break;
    }
  }

  err = xmessagetrans_close_file (&msgtrans_block);
  E (err);

  if (msgtrans_memory == NULL)
  {
    free (msgtrans_memory);
  }

  return EXIT_SUCCESS;
}
